package com.web.kongjian.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.entity.Weibo;
import com.web.kongjian.ao.WeiboAo;


@Controller
@RequestMapping("/weibo")
public class WeiboAction
{
	
	@Autowired
	private WeiboAo weiboAo;
	
	@RequestMapping(value="/send")
	public ModelAndView send(HttpServletRequest request,String content,HttpServletResponse response) throws IOException
	{
		 content = new String(content.getBytes("iso8859-1"),"UTF-8");
		 
		 Weibo weibo = new Weibo ();
		 weibo.setContent (content);
		 
		 weiboAo.send (weibo,request);
		 
		 String msg = "success";
		 response.setContentType("text/html;charset=gb2312");
		 PrintWriter out = response.getWriter();

		 out.print(msg);
		 out.flush();
		 out.close();
		
		return null;
	}
	
	@RequestMapping ("/comment")
	public ModelAndView comment(HttpServletRequest request,HttpServletResponse  response,long workId,long memberId,String content) throws IOException
	{
		content = new String(content.getBytes("iso8859-1"),"GBK");
		
		weiboAo.comment (memberId ,workId ,content ,request);
		 
		 String msg = "success";
		 response.setContentType("text/html;charset=gb2312");
		 PrintWriter out = response.getWriter();

		 out.print(msg);
		 out.flush();
		 out.close();
		
		return null;
	}
	
	@RequestMapping("/reply")
	public ModelAndView reply(HttpServletRequest request)
	{
		long commentId = Long.parseLong (request.getParameter ("commentId"));
		long comment_MId = Long.parseLong (request.getParameter ("comment_MId"));
		
		Map<String, Object> returnMap = new HashMap<String,Object>();
		returnMap.put ("commentId" ,commentId);
		returnMap.put ("comment_MId" ,comment_MId);
		
		return new ModelAndView("/kongjian/reply","returnMap",returnMap);
	}
	
	@RequestMapping("/sendReply")
	public ModelAndView sendReply(long commentId,long comment_MId,String content,HttpServletRequest request,HttpServletResponse response)
	{
		
		weiboAo.sendReply (request ,commentId ,comment_MId ,content);
		
		
		return new ModelAndView ("/kongjian/replySuccess");
	}
	
	@RequestMapping("/collection")
	public ModelAndView collection(long workId,long publisherId,HttpServletRequest request)
	{
		weiboAo.collect (workId ,publisherId,request);
		
		return null;
	}
	@RequestMapping("/parise")
	public ModelAndView parise(long workId,long publisherId,HttpServletRequest request)
	{
		weiboAo.praise  (workId ,publisherId,request);
		
		return null;
	}
	
	@RequestMapping("/transmit")
	public ModelAndView transmit(long workId,long publisherId,HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String string = weiboAo.transmit  (workId ,publisherId,request);
		if(ObjectUtils.notNull (string))
		{
			 response.setContentType("text/html;charset=gb2312");
			 PrintWriter out = response.getWriter();

			 out.print(string);
			 out.flush();
			 out.close();
		}
		
		return null;
	}
}
